package com.sea.supply.service.impl;


import com.sea.supply.entity.Buy;
import com.sea.supply.entity.ProduceDetail;
import com.sea.supply.mapper.ProduceDetailMapper;
import com.sea.supply.service.BuyService;
import com.sea.supply.service.ProduceDetailService;
import com.sea.supply.service.ProduceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 陈鸿飞
 */
@Service
public class ProduceDetailServiceImpl implements ProduceDetailService {

    @Autowired
    private ProduceDetailMapper produceDetailMapper;
    @Autowired
    private BuyService buyService;
    @Autowired
    private ProduceService produceService;

    @Override
    public int save(ProduceDetail produceDetail) {
        return produceDetailMapper.insert(produceDetail);
    }

    @Override
    public List<ProduceDetail> findByProduceId(Integer produceId) {
        return produceDetailMapper.findByProduceId(produceId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int usave(ProduceDetail produceDetail) {
        Integer buyId = produceDetail.getBuyId();
        Buy buy = buyService.findById(buyId);
        if(produceDetail.getProduceTotal()>buy.getBuyNumber()){
            throw new RuntimeException("数量不能大于待采购数量！");
        }
        produceDetail.setProduceSurplus(produceDetail.getProduceTotal());
        produceDetailMapper.update(produceDetail);
        return produceService.updateProduceTotal(produceDetail.getProduceId());
    }

    @Override
    public Integer updateFlag(ProduceDetail produceDetail) {
        return produceDetailMapper.update(produceDetail);
    }

    @Override
    public List<ProduceDetail> findReceiveNumber(String receivePo, String receiveSku,String channelName,String receiveDate) {
        return produceDetailMapper.findReceiveNumber(receivePo,receiveSku,channelName,receiveDate);
    }

    @Override
    public int updateReceiveNumber(Integer id) {
        return produceDetailMapper.updateReceiveNumber(id);
    }

    @Override
    public int findReceive(Integer produceId) {
        return produceDetailMapper.findReceive(produceId);
    }

    @Override
    public List<ProduceDetail> exportProduceDetail(Integer[] idList) {
        return produceDetailMapper.exportProduceDetail(idList);
    }

    @Override
    public int updateUseNumber(Integer useNumber, Integer id) {
        return produceDetailMapper.updateUseNumber(useNumber, id);
    }

    @Override
    public ProduceDetail getIdByCnd(String orderSku, String orderChannelName, String orderDate, String producePo) {
        return produceDetailMapper.getIdByCnd(orderSku, orderChannelName, orderDate, producePo);
    }

    @Override
    public ProduceDetail getWareDistribute(String orderSku, String orderChannelName, String orderDate, String producePo) {
        return produceDetailMapper.getWareDistribute(orderSku, orderChannelName, orderDate, producePo);
    }

    @Override
    public ProduceDetail findById(Integer id) {
        return produceDetailMapper.findById(id);
    }

    @Override
    public Integer updateSurplusNumber(Integer receiveNumber, Integer id) {
        return produceDetailMapper.updateSurplusNumber(receiveNumber, id);
    }

    @Override
    public Integer updateShort(ProduceDetail produceDetail) {
        return produceDetailMapper.updateShort(produceDetail);
    }

    @Override
    public Integer findByFirstProduce(String orderSku, String supplierName) {
        return produceDetailMapper.findByFirstProduce(orderSku,supplierName);
    }

    @Override
    public Integer updateReadyUseNumber(Integer useReadyNumber,Integer id) {
        return produceDetailMapper.updateReadyUseNumber(useReadyNumber,id);
    }


}
